package com.lanou.projecta.news.jokes;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.StackView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.projecta.R;
import com.lanou.projecta.tools.StringRequestUtf;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/22.
 */
public class JokesFragment extends Fragment{
    private JokeAdapter adapter;
    private JokesBase jokesBase;
    private RequestQueue queue;
    private List<JokesBase.段子Entity> base;
    private ListView listView;
    private String url="http://c.m.163.com/recommend/getChanListNews?channel=duanzi&size=20&passport=&" +
            "devId=X9N%2FozweCGhM6Gm2MRkxdQ%3D%3D&lat=nwP%2F7nhs70QSq%2FitwwFbpQ%3D%3D&lon=CLZNAVoophv" +
            "0OJvKM3ghsw%3D%3D&version=5.4.7&net=wifi&ts=1453687881&sign=GoB3yd%2B" +
            "IktyVtUzjrDoeoZ2IxGj8y4OwQnrVBfr65QV48ErR02zJ6%2FKXOnxX046I&encryption=1&canal=meizu_stor" +
            "e2014_news";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news_jokes,null);
        queue = Volley.newRequestQueue(getActivity());
      listView = (ListView) view.findViewById(R.id.jokes_listView);
        StringRequest request =new StringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("JokesFragment", response);
                base = new ArrayList<>();
                Gson gson = new Gson();
                jokesBase = gson.fromJson(response, JokesBase.class);
                base = jokesBase.get段子();
                adapter = new JokeAdapter(getActivity(), base);
                listView.setAdapter(adapter);


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);


        return view;
    }
}
